﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using BOL;
using BOL.UI;
using UtilityExtensions;
using System.Web.UI.DataVisualization.Charting;
using System.Data.SqlClient;
using Website.Logger;
using System.Web.UI.HtmlControls;
using System.Drawing;
using BOL.Ventas;

namespace UILGestion.VentaAdmin.Chart
{
    public partial class Chart07 : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {

                if (!this.IsPostBack)
                {
                    idProyecto.Value = (string)Request.QueryString["idProyecto"];
                    FormatForm();
                }
            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "") + "');", true);


            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }
        }


        public void FormatForm()
        {

           


        }
        public void LoadChart(int idProyecto,int tipoUI)
        {

            var bol = new UIBL();

            var descripciontipoUI = DbUtil.Db.UITipos.SingleOrDefault(p => p.idInternal == tipoUI);
            string titulo=descripciontipoUI==null?"VIVIENDAS":descripciontipoUI.descripcion.ToUpper();

                           
                            var etapas= (from a in DbUtil.Db.Etapas
                                   where 
                                   a.idProyecto==idProyecto
                                   & a.bitActivo==true
                                   select  new IndicadorEtapaVentaInfo()
                                   {
                                       etapa=a.descripcion,
                                       totalUI = a.UIs.Where(p => tipoUI==0?p.idTipoUI!=0 :p.idTipoUI == tipoUI).Count(),
                                       totalUIVen = a.UIs.Where(p => p.idEstado == 16 & (tipoUI == 0 ? p.idTipoUI != 0 : p.idTipoUI == tipoUI)).Count(),
                                       totalUISep=a.UIs.Where(p=>p.idEstado==2 & (tipoUI==0?p.idTipoUI!=0 :p.idTipoUI == tipoUI)).Count(),
                                       totalUIBlo = a.UIs.Where(p => p.idEstado == 17 & (tipoUI == 0 ? p.idTipoUI != 0 : p.idTipoUI == tipoUI)).Count(),
                                       totalUIDis = a.UIs.Where(p => p.idEstado == 1 & (tipoUI == 0 ? p.idTipoUI != 0 : p.idTipoUI == tipoUI)).Count(),
                                       totalVen = a.UIs.Where(p => p.idEstado == 16 & (tipoUI == 0 ? p.idTipoUI != 0 : p.idTipoUI == tipoUI)).Count() > 0 ? a.UIs.Where(p => p.idEstado == 16 & (tipoUI == 0 ? p.idTipoUI != 0 : p.idTipoUI == tipoUI)).Sum(l => l.OrdenVentaItems.Where(k => k.OrdenVenta.idEstado == 7).Sum(o => o.total)) : 0,  //soles  //VERIFICAR
                                       totalSep = a.UIs.Where(p => p.idEstado == 2 & (tipoUI == 0 ? p.idTipoUI != 0 : p.idTipoUI == tipoUI)).Count() > 0 ? a.UIs.Where(p => p.idEstado == 2 & (tipoUI == 0 ? p.idTipoUI != 0 : p.idTipoUI == tipoUI)).Sum(l => l.OrdenVentaItems.Where(k => k.OrdenVenta.idEstado == 5).Sum(o => o.total)) : 0,  //soles
                                       totalCobradoVen = a.UIs.Where(p => p.idEstado == 16 & (tipoUI == 0 ? p.idTipoUI != 0 : p.idTipoUI == tipoUI)).Count() > 0 ? a.UIs.Where(p => p.idEstado == 16 & (tipoUI == 0 ? p.idTipoUI != 0 : p.idTipoUI == tipoUI)).Sum(l => l.OrdenVentaItems.Where(k => k.OrdenVenta.idEstado == 7).Sum(o => o.OrdenVentaFormatos.Sum(i => i.totalPago))) : 0,  //soles
                                       totalCobradoSep = a.UIs.Where(p => p.idEstado == 2 & (tipoUI == 0 ? p.idTipoUI != 0 : p.idTipoUI == tipoUI)).Count() > 0 ? a.UIs.Where(p => p.idEstado == 2 & (tipoUI == 0 ? p.idTipoUI != 0 : p.idTipoUI == tipoUI)).Sum(l => l.OrdenVentaItems.Where(k => k.OrdenVenta.idEstado == 5).Sum(o => o.OrdenVentaFormatos.Sum(i => i.totalPago))) : 0,

                                       
                                   }).OrderBy(o=>o.etapa).ToList();


                                      //cabecera

                                TableRow trsup = new TableRow();
                                trsup.Font.Bold = true;
                                trsup.CssClass = "Heading secTitle AreaHeading ms-crm-List-Header";
                                TableCell td = new TableCell();
                                td.Text = "";
                                td.ColumnSpan = 2;
                                trsup.Controls.Add(td);
                                
                                td = new TableCell();
                                td.HorizontalAlign = HorizontalAlign.Center;
                                td.Text = "TOTAL";
                                td.Width = Unit.Point(100);
                                
                                trsup.Controls.Add(td);
                                foreach (var etapa in etapas)
                                {
                                    td = new TableCell();
                                    td.HorizontalAlign = HorizontalAlign.Center;
                                    td.Text = etapa.etapa.ToUpper();
                                    trsup.Controls.Add(td);

                                }
                                resumenTBL.Controls.Add(trsup);

                                trsup = new TableRow();
                                trsup.Font.Bold = true;
                                td = new TableCell();
                                td.HorizontalAlign = HorizontalAlign.Right;
                                td.ColumnSpan = 2;
                                td.Text = "TOTAL {0}".Fmt(titulo);
                                trsup.Controls.Add(td);
                                
                                td = new TableCell();
                                td.Text = string.Format("{0:N0}",etapas.Sum(p=>p.totalUI));
                                td.HorizontalAlign = HorizontalAlign.Right; 
                                trsup.Controls.Add(td);
                                foreach (var etapa in etapas)
                                {
                                    //  i++;
                                     td = new TableCell();
                                    td.HorizontalAlign = HorizontalAlign.Right;
                                    td.Attributes.Add("Width", "100px");
                                    
                                   
                                    td.Text = string.Format("{0:N0}",etapa.totalUI);

                                    trsup.Controls.Add(td);

                                }
                                 resumenTBL.Controls.Add(trsup);


            //-----------------
                                 //////----------------separacion------------
                                

                                 trsup = new TableRow();
                                 td = new TableCell();
                                 td.Text = "SEPARACION";
                                 trsup.Controls.Add(td);
                                 trsup.BackColor = Color.LightYellow;

                                 td = new TableCell();
                                 td.Text = "TOTAL  {0}".Fmt(titulo);
                                 td.HorizontalAlign = HorizontalAlign.Right;
                                 td.BackColor = Color.LightYellow;
                                 trsup.Controls.Add(td);

                                 td = new TableCell();
                                 td.Text = string.Format("{0:N0}", etapas.Sum(p => p.totalUISep));
                                 td.HorizontalAlign = HorizontalAlign.Right;
                                 td.Font.Bold = true;
                                 trsup.Controls.Add(td);
                                 foreach (var etapa in etapas)
                                 {
                                     //  i++;
                                     td = new TableCell();
                                     td.HorizontalAlign = HorizontalAlign.Right;
                                     td.Attributes.Add("Width", "100px");

                                     td.Text = string.Format("{0:N0}", etapa.totalUISep);

                                     trsup.Controls.Add(td);

                                 }
                                 resumenTBL.Controls.Add(trsup);

                                 trsup = new TableRow();
                                 td = new TableCell();
                                 td.Text = "";
                                 trsup.Controls.Add(td);
                                 trsup.BackColor = Color.LightYellow;
                                 td = new TableCell();
                                 td.Text = "TOTAL SEPARACION";
                                 td.HorizontalAlign = HorizontalAlign.Right;
                                 td.BackColor = Color.LightYellow;
                                 trsup.Controls.Add(td);

                                 td = new TableCell();
                                 td.Text = string.Format("S/. {0:N}", etapas.Sum(p => p.totalSep));
                                 td.HorizontalAlign = HorizontalAlign.Right;
                                 td.Font.Bold = true;
                                 trsup.Controls.Add(td);
                                 foreach (var etapa in etapas)
                                 {
                                     //  i++;
                                     td = new TableCell();
                                     td.HorizontalAlign = HorizontalAlign.Right;
                                     td.Attributes.Add("Width", "100px");

                                     td.Text = string.Format("S/. {0:N}", etapa.totalSep);

                                     trsup.Controls.Add(td);

                                 }
                                 resumenTBL.Controls.Add(trsup);

                                 trsup = new TableRow();
                                 trsup.BackColor = Color.LightYellow;
                                 td = new TableCell();
                                 td.Text = "";
                                 trsup.Controls.Add(td);
                                 td = new TableCell();
                                 td.Text = "TOTAL COBRADO";
                                 td.HorizontalAlign = HorizontalAlign.Right;
                                 td.ForeColor = Color.Green;
                                 td.BackColor = Color.LightYellow;
                                 trsup.Controls.Add(td);

                                 td = new TableCell();
                                 td.Text = string.Format("S/. {0:N}", etapas.Sum(p => p.totalCobradoSep));
                                 td.HorizontalAlign = HorizontalAlign.Right;
                                 td.ForeColor = Color.Green;
                                 td.Font.Bold = true;
                                 trsup.Controls.Add(td);
                                 foreach (var etapa in etapas)
                                 {
                                     //  i++;
                                     td = new TableCell();
                                     td.HorizontalAlign = HorizontalAlign.Right;
                                     td.Attributes.Add("Width", "100px");
                                     td.ForeColor = Color.Green;
                                     td.Text = string.Format("S/. {0:N}", etapa.totalCobradoSep);

                                     trsup.Controls.Add(td);

                                 }
                                 resumenTBL.Controls.Add(trsup);

                                 trsup = new TableRow();
                                 trsup.BackColor = Color.LightYellow;
                                 td = new TableCell();
                                 td.Text = "";
                                 trsup.Controls.Add(td);
                                 td = new TableCell();
                                 td.Text = "SALDO POR COBRAR";
                                 td.HorizontalAlign = HorizontalAlign.Right;
                                 td.ForeColor = Color.Red;
                                 td.BackColor = Color.LightYellow;
                                 trsup.Controls.Add(td);

                                 td = new TableCell();
                                 td.Text = string.Format("S/. {0:N}", etapas.Sum(p => p.totalXCobrarSep));
                                 td.HorizontalAlign = HorizontalAlign.Right;
                                 td.ForeColor = Color.Red;
                                 td.Font.Bold = true;
                                 trsup.Controls.Add(td);
                                 foreach (var etapa in etapas)
                                 {
                                     //  i++;
                                     td = new TableCell();

                                     td.HorizontalAlign = HorizontalAlign.Right;
                                     td.Attributes.Add("Width", "100px");
                                     td.ForeColor = Color.Red;
                                     td.Text = string.Format("S/. {0:N}", etapa.totalXCobrarSep);

                                     trsup.Controls.Add(td);

                                 }
                                 resumenTBL.Controls.Add(trsup);
                            











                                 trsup = new TableRow();
                                 //-------------Venta-----------
                                 trsup.BackColor = Color.LightGoldenrodYellow;
                                 td = new TableCell();
                                 td.Width = Unit.Point(100);                     
                                 td.Text = "VENTA";
                                 trsup.Controls.Add(td);

                                 td = new TableCell();
                                 
                                 td.Text = "TOTAL {0}".Fmt(titulo);
                                 td.HorizontalAlign = HorizontalAlign.Right;
                                 trsup.Controls.Add(td);

                                 td = new TableCell();
                                 td.Text = string.Format("{0:N0}", etapas.Sum(p => p.totalUIVen));
                                 td.HorizontalAlign = HorizontalAlign.Right; 
                                 trsup.Controls.Add(td);
                                 foreach (var etapa in etapas)
                                 {
                                     //  i++;
                                      td = new TableCell();
                                      td.HorizontalAlign = HorizontalAlign.Right;
                                      td.Attributes.Add("Width", "100px");
                                     
                                     td.Text =string.Format("{0:N0}", etapa.totalUIVen);

                                     trsup.Controls.Add(td);

                                 }
                                 resumenTBL.Controls.Add(trsup);


                                 trsup = new TableRow();
                                 trsup.BackColor = Color.LightGoldenrodYellow;
                                 td = new TableCell();
                                 td.Text = "";
                                 trsup.Controls.Add(td);
                                 td = new TableCell();
                                 td.Text = "TOTAL NEGOCIADO";
                                 td.HorizontalAlign = HorizontalAlign.Right;
                                 trsup.Controls.Add(td);


                                 td = new TableCell();
                                 td.Text = string.Format("S/. {0:N}", etapas.Sum(p => p.totalVen));
                                 td.Font.Bold = true;
                                 td.HorizontalAlign = HorizontalAlign.Right; 
                                 trsup.Controls.Add(td);
                                 foreach (var etapa in etapas)
                                 {
                                     //  i++;
                                      td = new TableCell();
                                      td.HorizontalAlign = HorizontalAlign.Right;
                                      td.Attributes.Add("Width", "100px");
                                     
                                     td.Text =string.Format("S/. {0:N}", etapa.totalVen);

                                     trsup.Controls.Add(td);

                                 }
                                 resumenTBL.Controls.Add(trsup);

                                 trsup = new TableRow();
                                 trsup.BackColor = Color.LightGoldenrodYellow;
                                 td = new TableCell();
                                 td.Text = "";
                                 trsup.Controls.Add(td);
                                 td = new TableCell();
                                 td.ForeColor = Color.Green;
                                 td.Text = "TOTAL COBRADO";
                                 td.HorizontalAlign = HorizontalAlign.Right;
                                 trsup.Controls.Add(td);

                                 td = new TableCell();
                                 td.Text = string.Format("S/. {0:N}", etapas.Sum(p => p.totalCobradoVen));
                                 td.Font.Bold = true;
                                 td.HorizontalAlign = HorizontalAlign.Right;
                                 td.ForeColor = Color.Green;
                                 trsup.Controls.Add(td);
                                 foreach (var etapa in etapas)
                                 {
                                     //  i++;
                                     td = new TableCell();
                                     td.HorizontalAlign = HorizontalAlign.Right;
                                     td.Attributes.Add("Width", "100px");
                                     td.ForeColor = Color.Green;
                                     td.Text = string.Format("S/. {0:N}", etapa.totalCobradoVen);

                                     trsup.Controls.Add(td);

                                 }
                                 resumenTBL.Controls.Add(trsup);


                                 trsup = new TableRow();
                                 trsup.BackColor = Color.LightGoldenrodYellow;
                                 td = new TableCell();
                                 td.Text = "";
                                 trsup.Controls.Add(td);
                                 td = new TableCell();
                                 td.Text = "SALDO POR COBRAR";
                                 td.HorizontalAlign = HorizontalAlign.Right;
                                 td.ForeColor = Color.Red;
                                 trsup.Controls.Add(td);

                                 td = new TableCell();
                                 td.Text = string.Format("S/. {0:N}", etapas.Sum(p => p.totalXCobrarVen));
                                 td.Font.Bold = true;
                                 td.HorizontalAlign = HorizontalAlign.Right;
                                 td.ForeColor = Color.Red;
                                 trsup.Controls.Add(td);
                                 foreach (var etapa in etapas)
                                 {
                                     //  i++;
                                     td = new TableCell();
                                     td.HorizontalAlign = HorizontalAlign.Right;
                                     td.Attributes.Add("Width", "100px");
                                     td.ForeColor = Color.Red;
                                     td.Text = string.Format("S/. {0:N}", etapa.totalXCobrarVen);

                                     trsup.Controls.Add(td);

                                 }
                                 resumenTBL.Controls.Add(trsup);




            ////////////////stock

                                 trsup = new TableRow();
                                 
                                 td = new TableCell();
                                 td.ColumnSpan = 2;
                                 td.Text = "DISPONIBLE:";
                                 td.HorizontalAlign = HorizontalAlign.Right; 
                                 
                                 td.Font.Bold = true;
                                 trsup.Controls.Add(td);

                                 td = new TableCell();
                                 td.Text = string.Format("{0:N0}", etapas.Sum(p =>p.totalUIDis));
                                 td.HorizontalAlign = HorizontalAlign.Right;
                                 td.Font.Bold = true;
                                 trsup.Controls.Add(td);
                                 foreach (var etapa in etapas)
                                 {
                                     //  i++;
                                     td = new TableCell();

                                     td.HorizontalAlign = HorizontalAlign.Right;
                                     td.Attributes.Add("Width", "100px");
                                     td.Font.Bold = true;
                                     td.Text = string.Format("{0:N}", etapa.totalUIDis);
                                    trsup.Controls.Add(td);

                                 }
                                 resumenTBL.Controls.Add(trsup);



                                 trsup = new TableRow();
                                
                                 td = new TableCell();
                                 td.ColumnSpan = 2;
                                 td.Text = "BLOQUEADO:";
                                 td.HorizontalAlign = HorizontalAlign.Right;
                                 
                                 td.Font.Bold = true;
                                 trsup.Controls.Add(td);

                                 td = new TableCell();
                                 td.Text = string.Format("{0:N0}", etapas.Sum(p => p.totalUIBlo));
                                 td.HorizontalAlign = HorizontalAlign.Right;
                                 td.Font.Bold = true;
                                 trsup.Controls.Add(td);
                                 foreach (var etapa in etapas)
                                 {
                                     //  i++;
                                     td = new TableCell();

                                     td.HorizontalAlign = HorizontalAlign.Right;
                                     td.Attributes.Add("Width", "100px");
                                     td.Font.Bold = true;
                                     td.Text = string.Format("{0:N}", etapa.totalUIBlo);
                                     trsup.Controls.Add(td);

                                 }
                                 resumenTBL.Controls.Add(trsup);

            
        }

        protected void itemGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
        {
            //e.Row.Cells[0].BackColor = Color.FromName("#99CCFF");

            if (e.Row.RowType == DataControlRowType.DataRow)
            {

                bool esTotal= ((UIResumenInfo)(e.Row.DataItem)).esTotal;

                
                if (esTotal)   
                {
                    e.Row.BackColor = Color.LightYellow;
                    e.Row.Font.Bold=true;
                }
                //ff000c
                

            }
        }
    }
}